﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastCtr;
using FastData;

namespace InjectMachine
{
    public class InjectOmroPLC : OmroPLC
    {
        private string _title;
        protected const string PCOnline = "PCOnline";
        protected const string PCScanSend = "PCScanSend";
        protected const string PCScanID = "PCScanID";
        protected const string PCWeighSend = "PCWeighSend";
        protected const string PCWeighID = "PCWeighID";
        protected const string PCWeighValue = "PCWeighValue";
        protected const string PCWeighInjectionValue = "PCWeighInjectionValue";
        protected const string PCWeighDatabaseUpdateOK = "PCWeighDatabaseUpdateOK";
     

        public InjectOmroPLC() : base() { }
        protected override void InitSetting()
        {
            #region =======基本配置
            HeartBeatCircleTime.Value = 5; //心跳频次
            PlcIpAddress.Value = "192.168.250.1";
            PlcCircletime.Value = 50;  //PLC循环监测值变化的周期
            HeartBeatAdress.Value = PCOnline; //PLC心跳地址
            #endregion

            #region =======监测数组变量
            PlcListenArryNameList.Value.Clear();
            PlcListenArryNameList.Value.Add(new ListenVar(PCOnline, 1, BaseType.Boolean));//心跳    //   PLC循环监测的数组，长度，类型
            PlcListenArryNameList.Value.Add(new ListenVar(PCScanSend, 7, BaseType.Int16));//7把扫码枪发送监测信号
            PlcListenArryNameList.Value.Add(new ListenVar(PCWeighSend, 8, BaseType.Int16));//8个电子秤发送监测信号
            #endregion

            #region =======需要观察的变量（非必须）
            DicVarDetail.Value.Clear();
            DicVarDetail.Value.Add($"{PCOnline}[0]", "心跳信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCScanSend}[0]", "扫码枪1触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCScanSend}[1]", "扫码枪2触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCScanSend}[2]", "扫码枪3触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCScanSend}[3]", "扫码枪4触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCScanSend}[4]", "扫码枪5触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCScanSend}[5]", "扫码枪6触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCScanSend}[6]", "扫码枪7触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[0]", "电子秤1触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[1]", "电子秤2触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[2]", "电子秤3触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[3]", "电子秤4触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[4]", "电子秤5触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[5]", "电子秤6触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[6]", "电子秤7触发信号");//供列表框观察的单个变量注释（非必须）
            DicVarDetail.Value.Add($"{PCWeighSend}[7]", "电子秤8触发信号");//供列表框观察的单个变量注释（非必须）
            #endregion
        }


        [Localizable(true)]
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public string Title
        {
            get
            {
                _title = Site.Name;
                groupBox1.Text = $"配置PLC连接和监测数组({_title})";
                return _title;
            }
            set
            {
                _title = value;
                groupBox1.Text = $"配置PLC连接和监测数组({_title})";
                if (!FastData.DesignHelper.IsDesignMode)
                {
                    if (PlcIpAddress == null)
                    {
                        try
                        {
                            DicVarDetail = new VarSave<Dictionary<string, string>>($"{Name}.DicVarDetail");
                            HeartBeatCircleTime = new VarSave<int>($"{Name}_WriteToPlcHeartBeatCircleTime");//心跳频次
                            PlcListVarname = new VarSave<List<string>>($"{Name}_PlcListVarname");
                            PlcIpAddress = new VarSave<string>($"{Name}_PlcIpAddress");
                            PlcCircletime = new VarSave<int>($"{Name}_PlcCircletime");
                            HeartBeatAdress = new VarSave<string>($"{Name}_PLCHeartBeatAdress");
                            PlcListenArryNameList = new VarSave<List<ListenVar>>($"{Name}_PlcListenArryNameList");
                            InitSetting();
                            PlcListVarname.Value.Clear();
                            PlcListVarname.Value = DicVarDetail.Value.Keys.ToList();

                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(Name + ": PLC配置参数初始化失败！\r\n" + exp);
                        }

                        Task.Factory.StartNew(() =>
                        {
                            StartReceivingMessage();
                        }, TaskCreationOptions.LongRunning);

                    }
                }
            }
        }

        //private void InitializeComponent()
        //{
        //    this.SuspendLayout();
        //    // 
        //    // InjectOmroPLC
        //    // 
        //    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        //    this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        //    this.Name = "InjectOmroPLC";
        //    this.Size = new System.Drawing.Size(1130, 752);
        //    this.ResumeLayout(false);
        //    this.PerformLayout();

        //}
    }
}
